from django.shortcuts import render, redirect,get_object_or_404, HttpResponse
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.decorators import login_required, user_passes_test
from .models import Project
from django.contrib.auth.decorators import login_required

@login_required
def project_list(request):
    projects = Project.objects.all()
    return render(request, 'project_list.html', {'projects': projects})
@login_required
def project_detail(request, pk):
    project = get_object_or_404(Project, pk=pk)
    return render(request, 'project_detail.html', {'project': project})

@login_required
def add_project(request):
    if request.method == 'POST':
        name = request.POST['name']
        description = request.POST['description']

        project = Project.objects.create(name=name, desc=description,creator=request.user)
        project.members.add(request.user)
        return redirect('projects')
    return render(request, 'add_project.html')

@login_required
@user_passes_test(lambda u: u.is_superuser)
def add_user(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('admin:index')
    else:
        form = UserCreationForm()
    return render(request, 'add_user.html', {'form': form})

def admin_user(request):
    return redirect('admin:index')


def user_logout(request):
    logout(request)
    return redirect('home')


# def home(request):
#     return render(request, 'home.html')
